<?php
class Admin_Model_ProductFeatureDetail extends Zend_Db_Table_Abstract {
	protected $_name = 'product_feature_detail';
	protected $_primary = array('productId','featureDetailId');
	protected $_dependentTables = array('Admin_Model_Images');
	protected $_referenceMap = array(
			'product'=>array(
					'columns' => array('productId'),
					'refTableClass' => 'Admin_Model_Product',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
					'onUpdate' => self::CASCADE,
			),
			'feature_detail'=>array(
					'columns' => array('featureDetailId'),
					'refTableClass' => 'Admin_Model_FeatureDetail',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
					'onUpdate' => self::CASCADE,
			)
			 
	);

	public function getProductFeatureDetail($param = NULL, $options = NULL) {
		if ($options == NULL) {
			$select = $this->select ();
		} 
		
		$result = $this->fetchAll ( $select );
		if ($result) {
			return $result->toArray ();
		} else {
			return null;
		}
	}

	public function getFetchPairs() {
		$query = "SELECT id, titleVi
                    FROM $this->_name";
		return $this->_db->fetchPairs ( $query );
	}

	public function getFeatureDetailOne($id, $options = null) {
		if ($options == null) {
			$select = $this->select ()
				->where ( 'id = ?', ( int ) $id );
		} elseif ($options ['task'] == 'feature') {
			$select = $this->select ()
				->setIntegrityCheck ( false )
				->from ( "{$this->_name} AS fd" )
				->join ( "feature AS fc", "fc.id = fd.FeatureId", array (
					'fc.categoryId' 
			) )
				->where ( 'fd.id = ?', ( int ) $id );
		}
		
		$result = $this->fetchRow ( $select );
		if ($result) {
			return $result->toArray ();
		} else {
			return null;
		}
	}

	public function getFeatureDetailByFeatureId($FeatureId) {
		$select = $this->select ()
			->where ( 'FeatureId = ?', ( int ) $FeatureId );
		
		$result = $this->fetchRow ( $select );
		if ($result) {
			return $result->toArray ();
		} else {
			return null;
		}
	}

	public function addItem($info) {
		try {
			$id = $this->insert ( $info );
			
			return $id;
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function updateItem($id, $info) {
		try {
			$result = $this->update ( $info, "id = " . $id );
			
			return $result;
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function deleteItem($params = null, $options = null) {
		try {
			if($options == null){
    			$productFeatureDetailRowset = $this->find($params);
    			$productFeatureDetail = $productFeatureDetailRowset->current();
    			$result = $productFeatureDetail->delete();
    		}elseif($options['task'] == 'delete-select'){
    			foreach($params as $key => $val){
    				$productFeatureDetailRowset = $this->find($params);
    				$productFeatureDetail = $productFeatureDetailRowset->current();
    				$result = $productFeatureDetail->delete();
    			}
    		}elseif($options['task'] == 'delete-all'){
    			$params = $this->getProductFeatureDetail();
    
    			foreach($params as $key => $val){
    				$productFeatureDetailRowset = $this->find($params);
    				$productFeatureDetail = $productFeatureDetailRowset->current();
    				$result = $productFeatureDetail->delete();
    			}
    		}
		
			return $result;
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function countItems() {
		$query = $this->select ()
			->from ( $this->_name, 'count(id) as total' );
		$result = $this->fetchRow ( $query );
		if ($result) {
			return $result->total;
		} else {
			return null;
		}
	}
}